#--------------------------------------------------------------------------------------------------------------------------------------------------------
#
# Mi-DevEnv			:	http://midevenv.codeplex.com 
#					:	(c) IT Bridge 2012
#					:	A concept originated by Chris Nurse
#
# Licence			:	GNU General Public License version 2 (GPLv2)
#					:	http://midevenv.codeplex.com/license
#
# Summary           :   Test Mi-DevEnv key functions
#
#--------------------------------------------------------------------------------------------------------------------------------------------------------

Remove-Module *
Import-Module MDE-Host

$cleanup = $true       # Delete machines if they exist
$createDC = $true      # Create a new domain controller
$createWS = $true      # Create a new web server with 2008 R2 template
$createWS2012 = $true  # Create a new web server with 2012 template

Write-Host "Mi-DevEnv Test Script"
Write-Host "---------------------"
Write-Host ""

if ($cleanup) {
    Write-Host "Delete test machines"
    Write-Host "--------------------"

    if ($createDC) {
        Write-Host "Domain controller"
        Remove-Machine -MachineName MdeDc -Confirm:$false
        }

    if ($createWS) {
        Write-Host "2008R2 Web Server"
        Remove-Machine -MachineName MdeApps -Confirm:$false
        }

    if ($createWS2012) {
        Write-Host "2012 Web Server"
        Remove-Machine -MachineName MdeApps2008 -Confirm:$false
        Remove-Machine -MachineName MdeApps2012 -Confirm:$false
        }
    }

Write-Host ""
Write-Host "Create machines"
Write-Host "---------------"
Write-Host "Virtual platform is $VirtualPlatform"
Write-Host ""

if ($createDC) {
    Write-Host "Create Domain Controller"
    Write-Host ""
    New-Machine -Verbose -MachineName MdeDc -DriveTemplate MDE-Server-2008R2 -Packages DC2008R2
    }

if ($createWS -or $createWS2012) {
    if ($createDC) {
        Write-Host "Waiting for domain controller setup to complete"
    Write-Host ""
        Start-Sleep -Seconds 900
        }

    $WsFeatures = "as-net-framework,Application-Server,Web-Common-Http,Web-Asp-Net,Web-Net-Ext,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Http-Logging,Web-Request-Monitor,Web-Basic-Auth,Web-Windows-Auth,Web-Filtering,Web-Performance,Web-Mgmt-Console,Web-Mgmt-Compat,WAS,MSMQ-Server,MSMQ-Directory,RSAT-AD-POWERSHELL"
    $WsPackages = "WS,SQL2012,CRM2011,VS2012U,Office2010,DVST"

    if ($createWS) {
        Write-Host "Create web server"
        Write-Host ""
        New-Machine -Verbose -Mnm MdeApps -Ram 4096 -Drv MDE-Server-2008R2 -Pkg $WsPackages -FTR $WsFeatures
        }
        
    if ($createWS2012) {
        #CRM2011 is not yet compatible with Windows Server 2012
        #There are compatibility issues between SQL Server 2008 and Windows Server 2012
        Write-Host "Create web server 2012"
        Write-Host ""
        New-Machine -Verbose -Mnm MdeApps2012 -Ram 4096 -Drv MDE-Server-2012 -Pkg "WS,SQL2012XAS,VS2010P,Office2013,DVST" -FTR $WsFeatures
        New-Machine -Verbose -Mnm MdeApps2008 -Ram 4096 -Drv MDE-Server-2008 -Pkg "WS,SQL2008" -FTR $WsFeatures
        }
    }

Write-Host "Remove mapped PowerShell drives"
Remove-DriveMappings